/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-11-22 16:19:19
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-06-04 16:44:12
 * @FilePath: \cxl-driving-training\src\views\clueManagement\clue\index.ts
 */
import { BaseData, BaseMethod, expandInterface } from '@/utils/basc-data'
import { tableConfig } from './config'
import { reactive } from 'vue'
import { FormInstance } from 'element-plus'
import { clueEdit, clueHighSeasPage, clueOperationInterface, cluePageInterface } from '@/apis/clue'
import { DICT_DATA } from '@/utils/dict-data'

export default class ClientLibraryData extends BaseData implements BaseMethod {
    dialog = reactive({
        visible: false,
        title: '详情',
        width: '1000px',
        id: '',
        type: 'create'
    })
    dialogForm = reactive({
        remark: '',
        followStatus: '0',
        id: ''
    })
    detailData: any = reactive({})
    detailDataPropName: any = reactive({
        customerId: '客户ID',
        customerName: '客户姓名',
        phone: '手机号码',
        viewedClassName: '查看班别',
        customerType: '客户类型',
        schoolName: '驾校',
        areaName: '区域',
        dataSource: '数据来源',
        systemSource: '系统来源',
        followStatus: '跟进状态',
        follower: '跟进人',
        createTime: '创建时间',
        lastViewedTime: '最后访问时间',
        signupTime: '报名时间',
        storeName: '报名门店',
        storeAreaName: '所属片区',
        referrerName: '推荐人姓名',
        referrerPhone: '推荐人手机号',
        transactionChannel: '成交渠道',
        updateTime: '更新时间'
    })
    followStatus = reactive<{ label: string; value: string }[]>([])
    activities = reactive<{ followMsg: string; followTime: string; follower: string }[]>([])
    searchData = {
        followStatus: '',
        dataSource: [],
        phone: '',
        customerName: '',
        referrer: '',
        time: [],
        customerType: '-1',
        systemSource: ''
    }
    constructor() {
        super(tableConfig)
        tableConfig.search.forEach(item => {
            if (item.key === 'customerType') {
                item.modelValue = this.searchData.customerType
            }
        })
        this.followStatus = DICT_DATA.get('followStatus')
        this.getData()
    }
    getData = (arg?: any) => {
        const params: cluePageInterface = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize
        }
        console.log('cluePageInterface', this.searchData)
        // if (this.searchData.followStatus !== '' && this.searchData.followStatus !== '-1') {
        //     params.followStatus = Number(this.searchData.followStatus)
        // }
        if (this.searchData.dataSource.length) {
            params.dataSources = this.searchData.dataSource.join(',')
        }
        if (this.searchData.phone !== '') {
            params.phone = this.searchData.phone + ''
        }
        if (this.searchData.customerName !== '') {
            params.customerName = this.searchData.customerName + ''
        }
        if (this.searchData.referrer !== '') {
            params.referrer = this.searchData.referrer + ''
        }
        // if (this.searchData.customerType !== '') {
        //     params.customerType = Number(this.searchData.customerType)
        // }
        if (this.searchData.systemSource !== '') {
            params.systemSource = Number(this.searchData.systemSource)
        }
        if (this.searchData.time.length) {
            params.startCreateTime = this.searchData.time[0]
            params.endCreateTime = this.searchData.time[1]
        }

        clueHighSeasPage(params).then(res => {
            if (Number(res.data.code) === 200) {
                this.pageData.table.data.length = 0
                this.pageData.pagination.total = res.data.result.total

                const arr: any[] = res.data.result.records

                arr.forEach((element: any) => {
                    element.status = Number(element.disable) === 0 ? true : false
                    const name = element.referrerName || ''
                    const phone = element.referrerPhone || '' ? '(' + element.referrerPhone + ')' : ''
                    element.referrerStr = name + phone
                    const areaName = element.areaName
                    const cityName = element.cityName

                    if (cityName && areaName) {
                        element.area = cityName + '/' + areaName
                    } else if (cityName && !areaName) {
                        element.area = cityName
                    } else if (!cityName && areaName) {
                        element.area = areaName
                    }

                    this.pageData.table.data.push(element)
                })
            }
        })
    }
    onSearch = (arg?: any) => {
        console.log(arg)
        this.searchData = arg
        this.getData()
    }
    onReset = (arg?: any) => {
        this.searchData = {
            followStatus: '',
            dataSource: [],
            phone: '',
            customerName: '',
            referrer: '',
            time: [],
            customerType: '',
            systemSource: ''
        }
        this.getData()
    }
    onPaginationChange = (arg?: any) => {
        this.pageData.pagination.pageNo = arg
        this.getData()
    }
    onPaginationSize = (arg?: any) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = arg
        this.getData()
    }
    onOperation = (arg?: any) => {
        const { type, data } = arg
        console.log(type, data)
        if (type === 'detail') {
            this.dialog.visible = true
            this.detailData = data
        }
    }
    //监听弹窗关闭
    onCloseDialog = () => {
        this.dialog.visible = false
    }
}
